package com.yunupmall.service.admin.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunupmall.api.fordVO.BrandVo;
import com.yunupmall.api.fordVO.CommiditySpecesVo;
import com.yunupmall.api.fordVO.CommodityVo;
import com.yunupmall.api.fordVO.KindPo;
import com.yunupmall.api.fordentity.*;
import com.yunupmall.entity.admin.commoditypojo.*;
import com.yunupmall.mapper.commoditymapper.CommodityMapper;
import com.yunupmall.service.admin.service.BrandService;
import com.yunupmall.service.admin.service.CommodityService;
import com.yunupmall.service.admin.service.KindService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class Commodityimpl extends ServiceImpl<CommodityMapper, Commodity> implements CommodityService {
    @Autowired
    private  CommodityMapper commodityMapper;
    @Autowired
    private  KindService kindService;
    @Autowired
    private  BrandService brandService;

    /**
     * 商品分页
     * @param commodityVo
     * @return
     */
    @Override
    public  Page<CommodityVo> pageData(CommodityVo commodityVo) {
        LambdaQueryWrapper<Commodity> wrapper = new LambdaQueryWrapper<Commodity>();
        if (ObjectUtils.isNotEmpty(commodityVo.getBrandId())){
            wrapper.eq(Commodity ::getBrandId,commodityVo.getBrandId());
        }
        if (ObjectUtils.isNotEmpty(commodityVo.getKindId())) {
            wrapper.eq(Commodity ::getKindId,commodityVo.getKindId());
        }
        if (ObjectUtils.isNotEmpty(commodityVo.getStatics())&& !Objects.equals(commodityVo.getStatics(),2)) {
            wrapper.eq(Commodity ::getStatics,commodityVo.getStatics());
        }
        if (ObjectUtils.isNotEmpty(commodityVo.getName())) {
            wrapper.like(Commodity ::getName,commodityVo.getName());
        }
        wrapper.eq(Commodity::getDeflag,0).orderByDesc(Commodity::getCreateTime);
        Page<Commodity> commodityPage = new Page<>(commodityVo.getPage(), commodityVo.getLimit());
        Page<Commodity> page = this.page(commodityPage, wrapper);
        List<CommodityVo> commodityVoList = new ArrayList<>();
        if (page.getRecords().size() > 0){
            final CommodityVo[] comm = new CommodityVo[1];
            page.getRecords().forEach(commodity -> {
                comm[0] =new CommodityVo();
                BeanUtils.copyProperties(commodity, comm[0]);
                KindPo kindPo = new KindPo();
                kindPo.setId(commodity.getKindId());
                List<Kind> kindPage = kindService.getList(kindPo);
                if (kindPage.size() > 0) {
                    comm[0].setKindName(kindPage.get(0).getName());
                }
                //品牌
                BrandVo brandVo = new BrandVo();
                brandVo.setId(commodity.getBrandId());
                List<Brand> brandPage = brandService.getList(brandVo);
                if (brandPage.size() > 0){
                    comm[0].setBrandName(brandPage.get(0).getName());
                }
                commodityVoList.add(comm[0]);
            });
        }
        Page<CommodityVo> commodityVoPage = new Page<>();
        commodityVoPage.setRecords(commodityVoList);
        commodityVoPage.setCurrent(commodityPage.getCurrent());
        commodityVoPage.setSize(commodityPage.getSize());
        commodityVoPage.setTotal(commodityPage.getTotal());
        return commodityVoPage;
    }

    /**
     * 商品删除
     * @param id
     * @return
     */
    @Override
    @Transactional
    public String del(Integer id) {
        if (ObjectUtil.isEmpty(id)){
            throw new RuntimeException("ID为空");
        }
        Commodity commodity = new Commodity();
        commodity.setId(id);
        commodity.setDeflag(1);
        if (this.updateById(commodity)){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }

    /**
     * 商品添加
     * @param commodityVo
     * @return
     */
    @Override
    @Transactional
    public Integer insertData(CommodityVo commodityVo) {
        if (ObjectUtil.isEmpty(commodityVo.getName())) {
            throw new RuntimeException("商品名称为空");
        }
        if (ObjectUtil.isEmpty(commodityVo.getPrice())) {
            throw new RuntimeException("商品价格");
        }
        if (ObjectUtil.isEmpty(commodityVo.getStock())) {
            throw new RuntimeException("商品库存为空");
        }
        if (ObjectUtil.isEmpty(commodityVo.getKindId())) {
            throw new RuntimeException("类别为空");
        }
        Commodity commodity = new Commodity();
        BeanUtils.copyProperties(commodityVo, commodity);
        commodity.setCreateTime(LocalDateTime.now());
        commodity.setDeflag(0);
        commodity.setStatics(1);
        this.save(commodity);
        return commodity.getId();
    }

    /**
     * 商品更新
     * @return
     */
    @Override
    @Transactional
    public String updateData(CommodityVo commodityVo) {
        if (ObjectUtil.isEmpty(commodityVo.getId())){
            throw new RuntimeException("ID为空");
        }
        Commodity commodity = new Commodity();
        BeanUtils.copyProperties(commodityVo, commodity);
        if(this.updateById(commodity)){
            return "更新成功";
        }else {
            throw new RuntimeException("更新失败");
        }
    }

    /**
     * list
     * @param commodityVo
     * @return
     */
    @Override
    public List<Commodity> newcommotityID(CommodityVo commodityVo) {
        LambdaQueryWrapper<Commodity> wrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(commodityVo.getId())) {
            wrapper.eq(Commodity ::getId,commodityVo.getId());
        }
        if (ObjectUtil.isNotEmpty(commodityVo.getName())) {
            wrapper.like(Commodity ::getName,commodityVo.getName());
        }
        if (ObjectUtil.isNotEmpty(commodityVo.getIsNew())) {
            int i = commodityMapper.newcommotityID();
            wrapper.eq(Commodity ::getId,i);
        }
        if (ObjectUtil.isNotEmpty(commodityVo.getKindId())){
            wrapper.eq(Commodity::getKindId,commodityVo.getKindId());
        }
        if (ObjectUtil.isNotEmpty(commodityVo.getStatics())) {
            wrapper.eq(Commodity ::getStatics,commodityVo.getStatics());
        }
        wrapper.eq(Commodity ::getDeflag,0);
        return  this.list(wrapper);

    }

    @Override
    public List<Commodity> selectDate(int thispage, int sizepage) {
        return commodityMapper.selectDate(thispage, sizepage);
    }

    @Override
    public KindCount getKindCount() {
        return commodityMapper.getKindCount();
    }

    @Override
    public List<Commodity> getcommoditywhereDate(CommodityWhere commodityWhere) {
        return commodityMapper.getcommoditywhereDate(commodityWhere);
    }

    @Override
    public int getDataCount(CommodityWhere commodityWhere) {
        return commodityMapper.getDataCount(commodityWhere);
    }


    @Override
    public List<KindUpDate> queryKindUpdata(Integer k_id, Integer thispage, Integer pagesize) {
        return commodityMapper.queryKindUpdata(k_id, thispage, pagesize);
    }

    @Override
    public int quetyCounts(Integer k_id) {
        return commodityMapper.quetyCounts(k_id);
    }



    @Override
    public int insertcommotitySpecs(List<ResultAdd> specs, Integer id) {
        return commodityMapper.insertcommotitySpecs(specs, id);
    }

    @Override
    public int AddceatecommotitySpecs(List<Specs> addcommtitySpecs, Integer cid) {
        return commodityMapper.AddceatecommotitySpecs(addcommtitySpecs, cid);
    }

    @Override
    public int insertCommtityData(AddCommotityimage addCommotityimage) {
        return commodityMapper.insertCommtityData(addCommotityimage);
    }

    @Override
    public List<SpecsName> quetyKind_specsList(Integer id) {
        return commodityMapper.quetyKind_specsList(id);
    }

    @Override
    public List<SpecsValues> querySpecsUpData(String SpecsName, Integer k_id) {
        return commodityMapper.querySpecsUpData(SpecsName, k_id);
    }

    @Override
    public int addcommotity_specsvalues(List<AllData> allData, Integer newSpecsID) {
        return commodityMapper.addcommotity_specsvalues(allData, newSpecsID);
    }

    @Override
    public List<SpecsCommodity> queryThisDataList(Integer c_id) {
        return commodityMapper.queryThisDataList(c_id);
    }
    @Override
    public int deleteCommtityThissku(Integer id) {
        return commodityMapper.deleteCommtityThissku(id);
    }


}
